package com.ruoyi.labdata.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.labdata.mapper.LabHolidayMapper;
import com.ruoyi.labdata.domain.LabHoliday;
import com.ruoyi.labdata.service.ILabHolidayService;
import com.ruoyi.system.utils.VacationDayCalculate;
import com.ruoyi.system.utils.VacationDayCalculate.OffDay;
import com.ruoyi.utils.LockUtil;

/**
 * 工作日Service业务层处理
 * 
 * @author winner pan
 * @date 2022-03-29
 */
@Service
public class LabHolidayServiceImpl implements ILabHolidayService {
	@Autowired
	private LabHolidayMapper labHolidayMapper;
	@Autowired
	private LockUtil lockUtil;
	/**
	 * 查询工作日
	 * 
	 * @param dayId
	 *            工作日ID
	 * @return 工作日
	 */
	@Override
	public LabHoliday selectLabHolidayById(Long dayId) {
		return labHolidayMapper.selectLabHolidayById(dayId);
	}

	/**
	 * 查询工作日列表
	 * 
	 * @param labHoliday
	 *            工作日
	 * @return 工作日
	 */
	@Override
	public List<LabHoliday> selectLabHolidayList(LabHoliday labHoliday) {
		return labHolidayMapper.selectLabHolidayList(labHoliday);
	}

	/**
	 * 新增工作日
	 * 
	 * @param labHoliday
	 *            工作日
	 * @return 结果
	 */
	@Override
	@Transactional
	public int insertLabHoliday(LabHoliday labHoliday) {
		if(labHoliday.getYear()==null){
			throw new RuntimeException("请输入需要生成工作日的年份!");
		}
		boolean lock = lockUtil.lock(getLockKey(labHoliday), 1000*60);
		if(!lock){
			throw new RuntimeException("系统忙请稍后生成!");
		}
		Integer isExist = this.labHolidayMapper.checkYear(labHoliday.getYear().toString());
		if(isExist!=null && isExist>0){
			throw new RuntimeException("该年份工作日已经存在，不能再生成.");
		}
		HashMap<String, OffDay> map = new VacationDayCalculate().yearVacationDay(labHoliday.getYear());
		Set<String> keySet = map.keySet();
		Date currentDate = DateUtils.getNowDate();
		for (String key : keySet) {
			OffDay v = map.get(key);
			LabHoliday day = new LabHoliday();
			day.setCreateTime(currentDate);
			day.setCreateBy(SecurityUtils.getUsername());
			day.setHolidayDate(labHoliday.getYear()+"-"+key);
			day.setHolidayName(v.getRemark());
			day.setHolidayFlag(v.isFlag()?"0":"1");
			labHolidayMapper.insertLabHoliday(day);
		}
		lockUtil.unLock(getLockKey(labHoliday));
		return 1;
	}

	private String getLockKey(LabHoliday labHoliday) {
		return this.getClass()+labHoliday.getYear().toString();
	}

	/**
	 * 修改工作日
	 * 
	 * @param labHoliday
	 *            工作日
	 * @return 结果
	 */
	@Override
	public int updateLabHoliday(LabHoliday labHoliday) {
		labHoliday.setCreateBy(SecurityUtils.getUsername());
		labHoliday.setCreateTime(DateUtils.getNowDate());
		return labHolidayMapper.updateLabHoliday(labHoliday);
	}

	/**
	 * 批量删除工作日
	 * 
	 * @param dayIds
	 *            需要删除的工作日ID
	 * @return 结果
	 */
	@Override
	public int deleteLabHolidayByIds(Long[] dayIds) {
		return labHolidayMapper.deleteLabHolidayByIds(dayIds);
	}

	/**
	 * 删除工作日信息
	 * 
	 * @param dayId
	 *            工作日ID
	 * @return 结果
	 */
	@Override
	public int deleteLabHolidayById(Long dayId) {
		return labHolidayMapper.deleteLabHolidayById(dayId);
	}
	
	public LabHoliday getLabHolidayByDate(String holidayDate){
		return labHolidayMapper.getLabHolidayByDate(holidayDate);
	}
}
